﻿# coding: utf-8
import weakref

import EntityParticleSystem
import SceneNode
import ParticleEmitter
import ParticleModifierColor
import ParticleModifierSize
import ParticleModifierTextureFrame

from InputHandler import CInputHandler
from SceneEditor import CSceneNode       

class CParticleModifier(HANDLE):
    PARTICLE_MOD_DESC_DICT = {
         ParticleEmitter.PMT_COLOR : TD.COLOR_MODIFIER,
         ParticleEmitter.PMT_SIZE : TD.SIZE_MODIFIER,
         ParticleEmitter.PMT_DIRECTION_FORCE : TD.DIRECTION_FORCE_MODIFIER,
         ParticleEmitter.PMT_TEXTURE_FRAME : TD.TEXTURE_FRAME_MODIFIER,
    }
    
    def __init__(self, emitter, modifierCoreHandle, type):
        HANDLE.__init__(self, modifierCoreHandle)
        
        self._emitterRef = weakref.ref(emitter)
        self._type = type
        self._name = CParticleModifier.PARTICLE_MOD_DESC_DICT.get(type, 'unknown')
        
    def GetKeyFrameByIndex(self, index):
        if self._type == ParticleEmitter.PMT_COLOR:
            return ParticleModifierColor.GetKeyFrameByIndex(self.GetCoreHandle(), index)
        elif self._type == ParticleEmitter.PMT_SIZE:
            return ParticleModifierSize.GetKeyFrameByIndex(self.GetCoreHandle(), index)
        elif self._type == ParticleEmitter.PMT_TEXTURE_FRAME:
            return ParticleModifierTextureFrame.GetKeyFrameByIndex(self.GetCoreHandle(), index)
            
        return None
        
    def GetKeyFrameCount(self):
        if self._type == ParticleEmitter.PMT_COLOR:
            return ParticleModifierColor.GetKeyFrameCount(self.GetCoreHandle())
        elif self._type == ParticleEmitter.PMT_SIZE:
            return ParticleModifierSize.GetKeyFrameCount(self.GetCoreHandle())
        elif self._type == ParticleEmitter.PMT_TEXTURE_FRAME:
            return ParticleModifierTextureFrame.GetKeyFrameCount(self.GetCoreHandle())
            
        return 0        
        
    def GetValue(self, pos):
        if self._type == ParticleEmitter.PMT_COLOR:
            return ParticleModifierColor.GetValue(self.GetCoreHandle(), pos)
        elif self._type == ParticleEmitter.PMT_SIZE:
            return ParticleModifierSize.GetValue(self.GetCoreHandle(), pos)
        elif self._type == ParticleEmitter.PMT_TEXTURE_FRAME:
            return ParticleModifierTextureFrame.GetValue(self.GetCoreHandle(), pos)
            
        return None        
        
    def SetKeyFrame(self, life, value):
        if self._type == ParticleEmitter.PMT_COLOR:
            color = WXCOLOR_TO_DWORD(value)
            ParticleModifierColor.SetKeyFrame(self.GetCoreHandle(), life, color)
        elif self._type == ParticleEmitter.PMT_SIZE:
            w, h = value
            ParticleModifierSize.SetKeyFrame(self.GetCoreHandle(), life, w, h)
        elif self._type == ParticleEmitter.PMT_TEXTURE_FRAME:
            ParticleModifierTextureFrame.SetKeyFrame(self.GetCoreHandle(), life, value)
            
class CParticleEmitter(HANDLE):
    def __init__(self, coreHandle):
        HANDLE.__init__(self, coreHandle)
        
        self._emitterModifierList = []
        self._particleModifierList = []
        
    def AddParticleModifier(self, modifierType):
        if not self.GetCoreHandle():
            return False
            
        ret, modifierCoreHandle = ParticleEmitter.AddParticleModifier(self.GetCoreHandle(), modifierType)
        if ret:
            self._particleModifierList.append(CParticleModifier(self, modifierCoreHandle, modifierType))
            return True
    
        return False  
        
    def RemoveParticleModifier(self, modifier):
        if not self.GetCoreHandle():
            return False
            
        self._particleModifierList.remove(modifier)
        ret = ParticleEmitter.RemoveParticleModifier(self.GetCoreHandle(), modifier.GetCoreHandle())
        return ret
                
class CParticleSystemEditor(CInputHandler):
    """ Particle system editor used to edit particle system entity """
    
    def __init__(self):
        """ Default class constructor """
        
        CInputHandler.__init__(self)
        
        self._emitterList = []
        
        # The weak ref to the editing particle system node.
        self._particleSystemNodeRef = None
        
        # The core handle of the editing particle system entity.
        self._particleSystemCoreHandle = 0
        
    def IsEditing(self):
        return self._particleSystemCoreHandle and self._particleSystemNodeRef()
        
    def New(self):
        self._particleSystemNodeRef, self._particleSystemCoreHandle = SCENE_EDITOR.CreateParticleSystemSceneNode('', True)
        
    def AddEmitter(self):
        if not self.IsEditing():
            return False
            
        ret, emitterHandle = EntityParticleSystem.AddEmitter(self._particleSystemCoreHandle)
        self._emitterList.append(CParticleEmitter(emitterHandle))
        return ret
        
    def RemoveEmitter(self, emitter):
        if not self.IsEditing():
            return False
            
        self._emitterList.remove(emitter)
        ret = EntityParticleSystem.RemoveEmitter(self._particleSystemCoreHandle, emitter.GetCoreHandle())
        return ret
        
    def GetTextureCutting(self):
        if not self.IsEditing():
            return False
            
        cutting = EntityParticleSystem.GetTextureCutting(self._particleSystemCoreHandle)
        return cutting
        
    def GetTextureFileName(self):
        if not self.IsEditing():
            return ''
            
        return EntityParticleSystem.GetTextureName(self._particleSystemCoreHandle)

    def SetTextureCutting(self, cutting):
        if not self.IsEditing():
            return
            
        EntityParticleSystem.SetTextureCutting(self._particleSystemCoreHandle, cutting)
        
    def SetTextureName(self, path):
        if not self.IsEditing():
            return
            
        EntityParticleSystem.SetTextureName(self._particleSystemCoreHandle, path)
                
__builtins__['PARTICLE_SYSTEM_EDITOR'] = CParticleSystemEditor()
